﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class SlideShowBLL
  {
    public List<SlideShow> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.SlideShows.OrderBy(p => p.Sort).ToList();
      }
    }
    public List<SlideShow> GetAll(int top)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.SlideShows.OrderBy(p => p.Sort).Take(top).ToList();
        }
    }

    public List<SlideShow> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.SlideShows.Where(p => p.isEnable == true).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<SlideShow> GetByPosition(int position, int top)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.SlideShows.Where(p => p.Position == position).Take(top).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<SlideShow> GetByPosition(int position, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.SlideShows.Where(p => p.Position == position && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList(); 
      }     
    }
   
    //Get by id

    public SlideShow GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.SlideShows.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public SlideShow GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.SlideShows.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Get max Sort

    public byte GetMaxSort(int position)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        SlideShow obj = data.SlideShows.Where(p=> p.Position == position).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        SlideShow objFirst = data.SlideShows.Where(p => p.Id == idFirst).FirstOrDefault();
        SlideShow objSecond = data.SlideShows.Where(p => p.Id == idSecond).FirstOrDefault();

        byte tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        SlideShow obj = data.SlideShows.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        SlideShow obj = data.SlideShows.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }
    public void Add(SlideShow obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.SlideShows.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    public void Update(SlideShow obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        SlideShow slideshow = data.SlideShows.Where(p => p.Id == obj.Id).FirstOrDefault();
        if (slideshow != null)
        {
          slideshow.Image = obj.Image;
          slideshow.Sort = obj.Sort;
          slideshow.isEnable = obj.isEnable;
          slideshow.Name = obj.Name;
          slideshow.Position = obj.Position;

          data.SubmitChanges();
        }
      }
    }

    public void Delete(int id)
    {
      using (var data= new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        SlideShow obj = data.SlideShows.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.SlideShows.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }

  }
}
